<template>
  <div id="Home">
    <home-header></home-header>
    <home-swiper :list="swiperList"></home-swiper>
    <home-icons :list="iconList"></home-icons>
    <recommend-icons :list="recommendList"></recommend-icons>
    <home-weekend :list="weekendList"></home-weekend>
  </div>
</template>

<script>
// import HomeHeader from './components/Header'//同步组件
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import RecommendIcons from './components/Recommend'
import HomeWeekend from './components/Weekend.vue'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
  name: 'Home',
  components: {
    HomeHeader: () => import('./components/Header'),//异步组件（分开加载，超过1MB使用）
    HomeSwiper,
    HomeIcons,
    RecommendIcons,
    HomeWeekend
  },
  data () {
    return {
      lastCity: '',
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: []
    }
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    doPost () {
      axios.post('/api/index.json',{
        city: this.city
      }).then((res) => {
        console.log(res)
      }).catch((err) => {
        console.log(err)
      })
    },
    getHomeInfo () {
      this.$get('/api/index.json',{
        params: {
          city: this.city
        }
      })
        .then((res) => {
          res = res.data
          if(res.ret && res.data){
            const data = res.data
            this.swiperList = data.swiperList
            this.iconList = data.iconList
            this.recommendList = data.recommendList
            this.weekendList = data.weekendList
          }
          console.log(res)
        }).catch((err) => {
          console.log(err)
        })
      
      // axios Post请求
      axios.post('/api/detail.json',{
        params: {
          id: this.lastCity
        }
      }).then((res) => {
        console.log(res)
      }).catch((res) => {
        console.log(res,'请求失败')
      })
    }
  },
  created () {
    this.doPost()
  },
  mounted () {
    this.lastCity = this.city
    this.getHomeInfo()
  },
  activated () {
    // 页面重新显示的时候执行
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getHomeInfo()
    }
  }
}
</script>

<style>
</style>
